package cn.wolfcode.p2p.base.domain;

import cn.wolfcode.p2p.util.Constants;
import cn.wolfcode.p2p.util.MD5;
import lombok.Getter;
import lombok.Setter;

import java.math.BigDecimal;

/**
 * 用户账户信息
 */
@Getter
@Setter
public class Account extends BaseDomain {

    //版本
    private Integer version = 0;
    //交易密码
    private String tradePassword;
    //可使用金额
    private BigDecimal usableAmount = Constants.ZERO;
    //冻结金额
    private BigDecimal freezedAmount = Constants.ZERO;
    //待收利息:投资人
    private BigDecimal unReceiveInterest = Constants.ZERO;
    //待收本金:投资人
    private BigDecimal unReceivePrincipal = Constants.ZERO;
    //待还金额:还款人
    private BigDecimal unReturnAmount = Constants.ZERO;
    //剩余信用额度
    private BigDecimal remainBorrowLimit = Constants.BORROWLIMIT;
    //总信用额度
    private BigDecimal borrowLimit = Constants.BORROWLIMIT;
    //账户校验
    private String verifyCode;

    //覆写get方法, 在insert和update 执行sql时 把verifyCode的值一并插入数据库
    public String getVerifyCode() {
        //变成字符串
        return MD5.encode((usableAmount.hashCode() + " " + freezedAmount.hashCode()));
    }

    //校验方法
    public boolean checkVerifyCode() {
        //注意: 是和数据库以前的值做对比
        return MD5.encode((usableAmount.hashCode() + " " + freezedAmount.hashCode())).equals(verifyCode);
    }

    //账户总额: 可用金额+待收利息+待收本金
    public BigDecimal getTotalAmount() {
        return usableAmount.add(unReceiveInterest).add(unReceivePrincipal);
    }

    //增加可用余额
    public void addUsableAmount(BigDecimal amount) {
        setUsableAmount(usableAmount.add(amount));
    }

    //减少可用余额
    public void subtractUsableAmount(BigDecimal amount) {
        setUsableAmount(usableAmount.subtract(amount));
    }

    //增加冻结余额
    public void addFreezedAmount(BigDecimal amount) {
        setFreezedAmount(freezedAmount.add(amount));
    }

    //减少冻结余额
    public void subtractFreezedAmount(BigDecimal amount) {
        setFreezedAmount(freezedAmount.subtract(amount));
    }


}
